package com.example.homework8.fragment;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.homework8.InputDialog;
import com.example.homework8.R;
import com.example.homework8.adapter.TagAdapter;
import com.example.homework8.bean.TagBean;
import com.example.homework8.database.MySQLHelper;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TagFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TagFragment extends Fragment implements TagAdapter.OnDelClickListener {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public TagFragment() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static TagFragment newInstance(String param1, String param2) {
        TagFragment fragment = new TagFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    private Toolbar mToolbar;
    private RecyclerView mRecyclerView;
    private FloatingActionButton fabAdd;
    private TagAdapter adapter;

    private MySQLHelper mySQLiteOpenHelpter;
    private SQLiteDatabase mDataBase;

    private int tag;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_tag, container, false);

        mToolbar = view.findViewById(R.id.mToolbar);
        mToolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().getSupportFragmentManager().popBackStack();
            }
        });
        mRecyclerView = view.findViewById(R.id.mRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new TagAdapter(tag);
        adapter.addOnDelClickListener(this);
        mRecyclerView.setAdapter(adapter);

        //初始化数据库
        mySQLiteOpenHelpter = new MySQLHelper(getActivity());
        mDataBase = mySQLiteOpenHelpter.getWritableDatabase();
        query();

        fabAdd = view.findViewById(R.id.fabAdd);
        fabAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
        return view;
    }
    private void showDialog(){
        final InputDialog dialog = new InputDialog(getActivity(),"请输入标签","");
        dialog.setCancelListerner(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setConfirmListerner(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                add(dialog.getContent());
                dialog.dismiss();
            }
        });
        dialog.show();
    }
    private void add(String content){
        ContentValues values = new ContentValues();
        values.put("describe",content);
        long res = mDataBase.insert("tag", null, values);  //插入一条数据
        if (res!=0){

            query();
        }else {
            Toast.makeText(getActivity(),"保存失败",Toast.LENGTH_SHORT).show();
        }
    }
    private void query() {
        //3.执行select 对应的sql语句 的方法  sql:sql语句    selectionArgs：占位符的参数值
        Cursor cursor = mDataBase.rawQuery("select * from tag", null);
        //4.解析结果集
        //判断结果集是否有效
        if (cursor != null && cursor.getCount() > 0) {
            ArrayList<TagBean> list = new ArrayList<>();
            while (cursor.moveToNext()) {//游标是否继续向下移动
                TagBean bean = new TagBean();
                //通过列的索引获取对应的数据
                bean.setId(cursor.getInt(cursor.getColumnIndex("tag_id")));
                bean.setText(cursor.getString(cursor.getColumnIndex("describe")));
                list.add(bean);
            }
            //关闭cursor
            cursor.close();
            adapter.setData(list);
        }else {
            Toast.makeText(getActivity(),"未获取到相关数据",Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onDelClick(int position, TagBean bean) {

    }

    @Override
    public void onItemClick(int position, TagBean bean) {

    }
}